package com.jmxcfc.blfsc.ninefour.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jmxcfc.blfsc.ninefour.entity.vo.NotifyResponseVo;
import com.jmxcfc.blfsc.ninefour.service.ISmsNotifyRecordService;
import com.jmxcfc.blfsc.ninefour.service.IVoiceNotifyRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 94语音:语音回调、短信回调通知回调控制器
 * 1、回调
 * (1)短信回调接口文档:
 挂机短信回调
 https://94ai.yuque.com/staff-kqoz0c/xed39g/zpcgwwo0dqkdqlgm
 *
 * (2)ai挂断回调(外呼回调)
 *
 * 2、加密版本的请求头:
 encrypt:AES/CBC/PKCS5PADDING
 sign:QvyagMjcEUI5M5E4TXqm9ol55h4=
 Charset:UTF-8
 appKey:37457cc0f06ff908
 uuid:38b44a00b0ed11ec00002c8bbbe49aff
 Content-Type:application/json
 timestamp:1648729344673

 * </p>
 * @author pbs
 */
@RestController
@RequestMapping("/ninefour")
@Slf4j
public class NineFourNotifyController {

    private ISmsNotifyRecordService smsNotifyRecordService;
    private IVoiceNotifyRecordService voiceNotifyRecordService;

    @Autowired
    public void setSmsNotifyRecordService(ISmsNotifyRecordService smsNotifyRecordService) {
        this.smsNotifyRecordService = smsNotifyRecordService;
    }

    @Autowired
    public void setVoiceNotifyRecordService(IVoiceNotifyRecordService voiceNotifyRecordService) {
        this.voiceNotifyRecordService = voiceNotifyRecordService;
    }

    /**
     * 挂机短信回调接口回调通知入口:
     * 1、请求
     * 请求数据格式: application/json;charset=utf-8
     * 请求方式：统一用POST方式 编码：UTF-8
     * 2、响应数据格式：application/json;charset=utf-8
     回调返回成功的标准是http状态码为200，并且返回的内容体的status参数也为200:
     {
     "status": 200,
     "message": "成功"
     }
     * 3、解密:
     从header里取 timestamp,然后企业自己的secret
     解密:AESUtil.cbcDecrypt
     * @param sign 头部参数:签名
     * @param timestamp 头部参数:时间戳
     * @param appKey 头部参数:客户服务key
     * @param notifyData 通知数据:加密(需要解密)
     * @return
     */
    @RequestMapping("/sms/notify")
    public JSONObject smsNotify(
                                 @RequestHeader(value = "sign", required = true) String sign,
                                 @RequestHeader(value = "timestamp", required = true) String timestamp,
                                 @RequestHeader(value = "appKey", required = true) String appKey,
                                 @RequestBody String notifyData) {
        try {
            log.info("94语音-挂机短信回调(挂短回调)回调通知,接收到头部请求参数 【sign】:{}  【timestamp】:{}  【appKey】:{} \r\n 请求体参数(加密的通知数据):{}", sign,timestamp,appKey,notifyData);
            JSONObject response =  JSON.parseObject(JSON.toJSONString(smsNotifyRecordService.smsNotifyHandler(sign, appKey, timestamp, notifyData)));
            log.info("94语音-挂机短信回调(挂短回调)回调通知,处理结束,同步响应结果:{}", response);
            return response;
        } catch (Exception e) {
            log.info("94语音-挂机短信回调(挂短回调)【sign】:{}, 【timestamp】:{},回调通知处理异常,异常信息:{}", sign, timestamp, e.getMessage(), e);
            return JSON.parseObject(JSON.toJSONString(NotifyResponseVo.fail("接收失败")));
        }
    }


    /**
     * ai挂断回调(外呼回调)
     * @param sign
     * @param timestamp
     * @param appKey
     * @param notifyData
     * @return
     */
    @RequestMapping("/voice/notify")
    public JSONObject voiceNotify(
            @RequestHeader(value = "sign", required = true) String sign,
            @RequestHeader(value = "timestamp", required = true) String timestamp,
            @RequestHeader(value = "appKey", required = true) String appKey,
            @RequestBody String notifyData) {
        try {
            log.info("94语音-ai挂断回调(外呼回调)回调通知,接收到头部请求参数 【sign】:{}, 【timestamp】:{}, 【appKey】:{},\r\n 请求体参数(加密的通知数据):{}", sign,timestamp,appKey,notifyData);
            JSONObject response =  JSON.parseObject(JSON.toJSONString(voiceNotifyRecordService.voiceNotifyHandler(sign, appKey, timestamp, notifyData)));
            log.info("94语音-ai挂断回调(外呼回调)回调通知,处理结束,同步响应结果:{}", response);
            return response;
        } catch (Exception e) {
            log.info("94语音-ai挂断回调(挂短回调),【sign】:{}, 【timestamp】:{},回调通知处理异常,异常信息:{}", sign, timestamp, e.getMessage(), e);
            return JSON.parseObject(JSON.toJSONString(NotifyResponseVo.fail("接收失败")));
        }
    }

}
